package com.cby.third.sample

import android.Manifest.permission.READ_EXTERNAL_STORAGE
import android.content.ActivityNotFoundException
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.cby.code.util.CbyLogUtils
import com.cby.third.cases.pdf.pdfui.PDFActivity
import com.cby.third.cases.pdf.pdfui.UIUtils

/**
 * @description: PdfViewerActivity
 *
 * @author ChenBaoyang
 * @date 2021/12/1 19:44
 */
class PdfViewerActivity : AppCompatActivity() {

    private val REQUEST_CODE = 42
    private val PERMISSION_CODE = 42042

    private lateinit var backBtn: Button
    private lateinit var fileBtn: Button
    private lateinit var assetsBtn: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        UIUtils.initWindowStyle(window, supportActionBar)
        setContentView(R.layout.activity_pdf_viewer)
        initView()
        setEvent()
    }

    private fun initView() {
        backBtn = findViewById(R.id.btn_back)
        fileBtn = findViewById(R.id.btn_file)
        assetsBtn = findViewById(R.id.btn_assets)
    }

    private fun setEvent() {
        backBtn.setOnClickListener { this@PdfViewerActivity.finish() }
        fileBtn.setOnClickListener(View.OnClickListener { pickFile() })
        assetsBtn.setOnClickListener(View.OnClickListener {
            val intent = Intent(this@PdfViewerActivity, PDFActivity::class.java)
            intent.putExtra("AssetsPdf", "swift.pdf")
            this@PdfViewerActivity.startActivity(intent)
        })
    }

    private fun pickFile() {
        val permissionCheck: Int = ContextCompat.checkSelfPermission(
            this,
            READ_EXTERNAL_STORAGE
        )
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(
                this, arrayOf(READ_EXTERNAL_STORAGE),
                PERMISSION_CODE
            )
            return
        }
        launchPicker()
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        if (requestCode == PERMISSION_CODE) {
            if (grantResults.isNotEmpty()
                && grantResults[0] == PackageManager.PERMISSION_GRANTED
            ) {
                launchPicker()
            }
        }
    }

    private fun launchPicker() {
        val intent = Intent()
        intent.type = "application/pdf"
        intent.action = "android.intent.action.GET_CONTENT"
        intent.addCategory("android.intent.category.OPENABLE")
        startActivityForResult(intent, REQUEST_CODE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            when (requestCode) {
                REQUEST_CODE -> {
                    val uri: Uri? = data?.data
                    Toast.makeText(this@PdfViewerActivity, uri.toString(), Toast.LENGTH_SHORT).show()
                    val i = Intent(this, PDFActivity::class.java)
                    i.data = uri
                    CbyLogUtils.flog("----uri---"+uri.toString())
                    this@PdfViewerActivity.startActivity(i)
                }
            }
        }
    }

}
